package cn.iocoder.yudao.module.shop.statistics.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.api.trade.order.TradeOrderApi;
import cn.iocoder.yudao.module.api.trade.order.vo.TradeOrderSummaryVO;
import cn.iocoder.yudao.module.shop.statistics.service.MerchantCenterSummaryService;
import cn.iocoder.yudao.module.shop.statistics.service.ShopStatisticsService;
import cn.iocoder.yudao.module.shop.statistics.vo.MerchantCenterSummaryVO;
import cn.iocoder.yudao.module.shop.statistics.vo.ShopStatisticsRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 店铺统计")
@RestController
@RequestMapping("/shop/statistics")
@Validated
@Slf4j
public class AppShopStatisticsController {

    @Resource
    private MerchantCenterSummaryService merchantCenterSummaryService;
    @Resource
    private ShopStatisticsService shopStatisticsService;

    @Resource
    private ShopInfoApi shopInfoApi;
    @Resource
    private TradeOrderApi tradeOrderApi;

    @GetMapping("/summary")
    @Operation(summary = "获得商家数据汇总")
    @PreAuthorize("@mm.isMerchant()")
    public CommonResult<MerchantCenterSummaryVO> getMySummary() {
        Long userId = getLoginUserId();
        Long shopId = shopInfoApi.getShopIdByMemberUserId(userId);
        MerchantCenterSummaryVO summaryVO = merchantCenterSummaryService.getMySummary(userId, shopId);
        return success(summaryVO);
    }

    @GetMapping("/order-count")
    @Operation(summary = "获得订单数据汇总")
    @PermitAll // 在首页调用，允许未登录
    public CommonResult<TradeOrderSummaryVO> getMyOrderCount() {
        Long userId = getLoginUserId();
        if (userId == null || userId <= 0) {
            return success(new TradeOrderSummaryVO());
        }
        Long shopId = shopInfoApi.getShopIdByMemberUserId(userId);
        if (shopId == null || shopId <= 0) {
            return success(new TradeOrderSummaryVO());
        }
        TradeOrderSummaryVO summaryVO = tradeOrderApi.getOrderSummaryByShopId(shopId);
        return success(summaryVO);
    }

    @GetMapping("/today")
    @Operation(summary = "获得今日店铺统计")
    @PreAuthorize("@mm.isMerchant()")
    public CommonResult<ShopStatisticsRespVO> getMyShopTodayStatistics() {
        Long shopId = shopInfoApi.getShopIdByMemberUserId(getLoginUserId());
        return success(shopStatisticsService.getStatistics(shopId, LocalDate.now()));
    }

}
